<?php
class InformationModel extends Model{
	// insert
	public function add_information($data)
	{  
		import('ORG.Net.UploadFile');
        // 实例化上传类
        $upload = new UploadFile();
        // 设置附件上传大小
        // $upload->maxSize  = 3145728 ;
        // 设置附件上传类型
        // $upload->allowExts  = '';
       

        //设置附件上传目录
        $upload->savePath='Public/Uploads/information/';

        if($upload->upload() && $data['information_url_type'] == 1){
            // 上传成功 获取上传文件信息
            $info =  $upload->getUploadFileInfo();
            $data['time_insert'] = time();
            $data['time_update'] = 0;
            $data['time_delete'] = -1;
            $data['information_size'] = round($info[0]['size']/1000000 , 2);
            $data['information_type'] = $info[0]['extension'];
            $type = array("mp4" , "pdf" , "txt" , "mp3" , "docx","doc" ,"jpeg" ,"jpg" ,"png" , "gif" , "xls" ,"xlsx");
            if(in_array($data['information_type'] , $type)){
                // $data['information_image'] = $info[0]['savepath'].$info[0]['savename'];
                $data['information_url_type'] = 1;
                $data['information_url_editor'] =C('DOMAIN_NAME').__ROOT__.'/'.$upload->savePath.$info[0]['savename']; 
                $data['information_category'] = 1;
                return M('information' , '' , 'DB_MEETING')->add($data);
            }
        }elseif($data['information_url_type'] == 2)
        {   
            $info =  $upload->getUploadFileInfo();
            // $data['information_image'] = $info[0]['savepath'].$info[0]['savename'];
            $data['information_url_type'] = 1 ;
            $a_array = get_headers($data['information_url_editor'],true);
            $data['information_size'] = round($a_array['Content-Length']/1000000 , 2); 
            $a = end(explode('/' , $a_array['Content-Type']));

            $type = array("mp4" , "pdf" ,"txt" , "mp3" , "docx" ,"doc","jpeg" ,"jpg" ,"png" , "gif" ,"xls" ,"xlsx");
            
            if(in_array($a , $type))
            {
                $data['information_type'] = $a;
                $data['time_insert'] = time();
                $data['time_update'] = 0;
                $data['time_delete'] = -1;
                $data['information_category'] = 1;
                $data['information_url_editor'] = C('DOMAIN_NAME').__ROOT__.'/'.$upload->savePath.$info[0]['savename'];
                return M('information' , '' , 'DB_MEETING')->add($data);
            }
		}
	}    
  
   

    public function get_channel(){
        return M('information_channel','','DB_MEETING')->where('time_insert>time_delete')->select();
    }

	public function add_information_channel($data)
	{
		$data['time_insert'] = time();
        $data['time_update'] = 0;
        $data['time_delete'] = -1;
        return M('information_channel' , '' , 'DB_MEETING')->add($data);
	}


    // 更新
	public function update_information($data)
	{
        // 实例化上传类
        import('ORG.Net.UploadFile');
        $upload = new UploadFile();
        $upload->savePath='Public/Uploads/information/';
        if($upload->upload() && $data['information_url_type'] == 1){
            // 上传成功 获取上传文件信息
            $info =  $upload->getUploadFileInfo();
            $data['time_update'] = time();
            $data['information_size'] = round($info[0]['size']/1000000 , 2);
            $data['information_type'] = $info[0]['extension'];
            $type = array("mp4" , "pdf" , "txt" , "mp3" , "docx" , "doc","jpeg" ,"jpg" ,"png" , "gif" ,"xls" , "xlsx");
            if(in_array($data['information_type'] , $type)){
                $data['information_url_type'] = 1;
                $data['information_url_editor'] =C('DOMAIN_NAME').__ROOT__.'/'.$upload->savePath.$info[0]['savename']; 
                $data['information_category'] = 1;
                return M('information' ,'' ,'DB_MEETING')->where('information_id='.$data['information_id'])->save($data);
            }
        }elseif($data['information_url_type'] == 2)
        {   
            $info =  $upload->getUploadFileInfo();
            $data['information_url_type'] = 1 ;
            $a_array = get_headers($data['information_url_editor'],true);
            $data['information_size'] = round($a_array['Content-Length']/1000000 , 2); 
            $a = end(explode('/' , $a_array['Content-Type']));
            $type = array("mp4" , "pdf" ,"txt" , "mp3" , "docx" , "doc","jpeg" ,"jpg" ,"png" , "gif" ,"xls" , "xlsx");
            if(in_array($a , $type))
            {
                $data['information_type'] = $a;
                $data['time_update'] = time();
                $data['information_category'] = 1;
                $data['information_url_editor'] = C('DOMAIN_NAME').__ROOT__.'/'.$upload->savePath.$info[0]['savename'];
                $data['time_update'] = time();
                return M('information' ,'' ,'DB_MEETING')->where('information_id='.$data['information_id'])->save($data);
            }
        }
	}

    //删除 
	public function to_del_channel($data)
	{
		return M('information_channel','','DB_MEETING')->where('channel_id='.$data['channel_id']) ->setField('time_delete' , time());
	}

	public function to_del($data)
	{
		 return M('information','','DB_MEETING')->where('information_id='.$data['information_id']) ->setField('time_delete' , time());
	}
}

?>